#include "TestForm.h"
#include "ui_TestForm.h"
#include <QWidget>
#include <QSpacerItem>
#include "RandHeadLabel.h"
CTestForm::CTestForm(QWidget *parent, CommonWndFlags WndFlag):CCommonWidget(parent, WndFlag),ui(new Ui::CTestForm)
{
    ui->setupUi(this);
}

CTestForm::~CTestForm()
{
    Destroy();
}

void CTestForm::Create()
{
    setObjectName("TestForm");
    //创建系统栏题栏等
    CrateSystemTitle(ui->verticalLayout);
    CrateSystemButton(enum_UXApplication);
    SetTitle(QString::fromUtf8("PK 结果详情"));
    connect(this, SIGNAL(OnClose()), this, SLOT(Close()));
    BuildUI();
}

void CTestForm::Destroy()
{

}

void CTestForm::Close()
{
    close();
}


void CTestForm::BuildUI()
{
    m_iUiHeight = 46;  //标题栏高度
    //最上层spacer
    QSpacerItem *headSpacer;
    headSpacer = new QSpacerItem(20, 28, QSizePolicy::Preferred, QSizePolicy::Fixed);
    ui->verticalLayout_4->addItem(headSpacer);
    m_iUiHeight += 28;

    //头像层
    QFrame* headIconFrame = new QFrame(this);
    headIconFrame->setFixedSize(this->width(),77);
    QHBoxLayout* headIconLayout = new QHBoxLayout(headIconFrame);
    headIconLayout->setAlignment(Qt::AlignCenter);
    headIconLayout->setSpacing(0);
    headIconLayout->setMargin(0);
    headIconFrame->setLayout(headIconLayout);

    QPen pen;
    pen.setWidth(2);
    QString strRgb;
    strRgb = QString::number(0xff8383, 16);
    pen.setColor(QColor(strRgb.toUInt(NULL, 16)));
    m_iUiHeight += 16;

    CRandHeadLabel* lbHeadIcon = new CRandHeadLabel(this);
    lbHeadIcon->setFixedSize(77,77);
    lbHeadIcon->SetShowBorder(true);
    lbHeadIcon->SetBoderPen(pen);
    lbHeadIcon->SetImagePath(":/skin/head2.png");
    headIconLayout->addWidget(lbHeadIcon);
    ui->verticalLayout_4->addWidget(headIconFrame);
    m_iUiHeight += 77;

    //头像与昵称间距
    QSpacerItem *headToNickNameSpacer;
    headToNickNameSpacer = new QSpacerItem(20, 10, QSizePolicy::Preferred, QSizePolicy::Fixed);
    ui->verticalLayout_4->addItem(headToNickNameSpacer);
    m_iUiHeight += 10;

    //昵称栏
    QFrame* nickNameFrame = new QFrame(this);
    nickNameFrame->setFixedSize(this->width(),30);
    QHBoxLayout* nickNameLayout = new QHBoxLayout(nickNameFrame);
    nickNameLayout->setAlignment(Qt::AlignCenter);
    nickNameLayout->setSpacing(0);
    nickNameLayout->setMargin(0);
    nickNameFrame->setLayout(nickNameLayout);

    QLabel* lbNickName = new QLabel(this);
    lbNickName->setObjectName("pkDetailNickname");
    lbNickName->setFixedSize(this->width(),30);
    nickNameLayout->addWidget(lbNickName);
    lbNickName->setAlignment(Qt::AlignCenter);
    lbNickName->setText(QString::fromUtf8("安然家的千与千寻"));
    ui->verticalLayout_4->addWidget(nickNameFrame);
    m_iUiHeight += 30;

#if 0
    //昵称与灵力变化间距
    QSpacerItem *nickNameToGamaChangeSpacer;
    nickNameToGamaChangeSpacer = new QSpacerItem(20, 20, QSizePolicy::Preferred, QSizePolicy::Fixed);
    ui->verticalLayout_4->addItem(nickNameToGamaChangeSpacer);
    m_iUiHeight += 20;
#else
    //昵称到mvp间距
    QSpacerItem *nickNameToMVPSpacer;
    nickNameToMVPSpacer = new QSpacerItem(20, 14, QSizePolicy::Preferred, QSizePolicy::Fixed);
    ui->verticalLayout_4->addItem(nickNameToMVPSpacer);
    m_iUiHeight += 14;

    //mvp条
    QFrame* mvpFrame = new QFrame(this);
    mvpFrame->setFixedSize(this->width(),35);
    QHBoxLayout* mvpLayout = new QHBoxLayout(mvpFrame);
    mvpLayout->setAlignment(Qt::AlignCenter);
    mvpLayout->setSpacing(0);
    mvpLayout->setMargin(0);
    mvpLayout->setContentsMargins(0,0,0,0);
    mvpFrame->setLayout(mvpLayout);
    ui->verticalLayout_4->addWidget(mvpFrame);
    m_iUiHeight += 35;

    {
        //现实mvp
        QFrame* realMvpFrame = new QFrame(mvpFrame);
        realMvpFrame->setObjectName("mvpUserLayout");
        realMvpFrame->setFixedSize(mvpFrame->width(),mvpFrame->height());
        mvpLayout->addWidget(realMvpFrame);

        QHBoxLayout* realMvpLayout = new QHBoxLayout(realMvpFrame);
        realMvpLayout->setAlignment(Qt::AlignCenter);
        realMvpLayout->setSpacing(5);
        realMvpLayout->setContentsMargins(3,0,0,0);
        realMvpFrame->setLayout(realMvpLayout);


        CRandHeadLabel* lbMvpHeadIcon = new CRandHeadLabel(realMvpFrame);
        lbMvpHeadIcon->setFixedSize(27,27);
        lbMvpHeadIcon->SetShowBorder(true);
        lbMvpHeadIcon->SetBoderPen(pen);
        lbMvpHeadIcon->SetImagePath(":/skin/head1.jpg");
        realMvpLayout->addWidget(lbMvpHeadIcon);


        QLabel* lbMvpNickName = new QLabel(realMvpFrame);
        lbMvpNickName->setText(QString::fromUtf8("段位段位"));
        realMvpLayout->addWidget(lbMvpNickName);

        QLabel* lbMvpIcon = new QLabel(realMvpFrame);
        QPixmap pixmap;
        pixmap.load(":/skin/mvp.png");
        QPixmap fitpixmap = pixmap.scaled(27, 27, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        lbMvpIcon->setPixmap(pixmap);
        realMvpLayout->addWidget(lbMvpIcon);

        QString timeMVP = QString("x%1").arg(51);
        QLabel* lbMvpTime = new QLabel(realMvpFrame);
        lbMvpTime->setObjectName("mvpTime");
        lbMvpTime->setText(QString("%1").arg(timeMVP));
        realMvpLayout->addWidget(lbMvpTime);

        QSpacerItem *mvpVerticalSpacer;
        mvpVerticalSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Preferred);

        realMvpLayout->addItem(mvpVerticalSpacer);

        QFontMetrics fm1(lbMvpNickName->font());
        QRect rec1 = fm1.boundingRect(lbMvpNickName->text());

        QFontMetrics fm2(lbMvpTime->font());
        QRect rec2 = fm2.boundingRect(timeMVP);


        int textWidth = lbMvpHeadIcon->width()+rec1.width()+pixmap.width()+rec2.width()+38;
        realMvpFrame->setFixedWidth(textWidth);

    }

    //mvp到灵力变化间距
    QSpacerItem *mvpToGamaChangeSpacer;
    mvpToGamaChangeSpacer = new QSpacerItem(20, 16, QSizePolicy::Preferred, QSizePolicy::Fixed);
    ui->verticalLayout_4->addItem(mvpToGamaChangeSpacer);
    m_iUiHeight += 16;
#endif

    //灵力变化条
    ui->verticalLayout_4->addWidget(BuildScoreDetailItem2(this,this->width(),44,QString::fromUtf8("灵力变化"),"12(-89)",24,28));
    m_iUiHeight += 44;

    //得分详情表
    BuildScoreDetailTable(false);

    //得分表下间距
    QSpacerItem *scoreDetailToTextAreaSpacer;
    scoreDetailToTextAreaSpacer = new QSpacerItem(20, 12, QSizePolicy::Preferred, QSizePolicy::Fixed);
    ui->verticalLayout_4->addItem(scoreDetailToTextAreaSpacer);

    m_iUiHeight += 12;

    QFrame *tmpFrame = BuildScoreDetailItem2(this,this->width(),42,QString::fromUtf8("连胜场次"),"12(+1)",24,28);
    ui->verticalLayout_4->addWidget(tmpFrame);
    QLabel* tmpLabel = tmpFrame->findChild<QLabel*>(QString("itemValue"));

    ui->verticalLayout_4->addWidget(BuildScoreDetailItem2(this,this->width(),42,QString::fromUtf8("段位变化"),"12(+122)",24,28));
    ui->verticalLayout_4->addWidget(BuildScoreDetailItem2(this,this->width(),42,QString::fromUtf8("主播奖励"),"12(-89)",24,28));
    m_iUiHeight += 42*3;

    //最下层spacer
    QSpacerItem *verticalSpacer;
    verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Preferred, QSizePolicy::Expanding);
    ui->verticalLayout_4->addItem(verticalSpacer);

    setFixedHeight(m_iUiHeight);
}

void CTestForm::BuildScoreDetailTable(bool bWin)
{
    //表格外框
    QFrame* scoreDetailFrame = new QFrame(this);
    if(bWin)
        scoreDetailFrame->setFixedSize(this->width(),129);
    else
        scoreDetailFrame->setFixedSize(this->width(),96);

    m_iUiHeight+= scoreDetailFrame->height();

    QHBoxLayout* scoreDetailLayout = new QHBoxLayout(scoreDetailFrame);
    scoreDetailLayout->setAlignment(Qt::AlignCenter);
    scoreDetailLayout->setSpacing(0);
    scoreDetailLayout->setMargin(0);
    scoreDetailLayout->setContentsMargins(0,0,0,0);
    scoreDetailFrame->setLayout(scoreDetailLayout);
    ui->verticalLayout_4->addWidget(scoreDetailFrame);

    //真实表格
    QFrame* scoreDetailInnerFrame = new QFrame(scoreDetailFrame);
    scoreDetailInnerFrame->setFixedSize(321,scoreDetailFrame->height());
    scoreDetailInnerFrame->setObjectName("realScoreDetailTable");

    QHBoxLayout* scoreDetailInnerLayout = new QHBoxLayout(scoreDetailInnerFrame);
    scoreDetailInnerLayout->setAlignment(Qt::AlignCenter);
    scoreDetailInnerLayout->setSpacing(0);
    scoreDetailInnerLayout->setMargin(0);
    scoreDetailInnerLayout->setContentsMargins(0,18,0,18);
    scoreDetailInnerFrame->setLayout(scoreDetailInnerLayout);

    //左侧分数详情面板
    QFrame* leftScore = new QFrame(scoreDetailInnerFrame);
    leftScore->setObjectName("leftScore");
    leftScore->setFixedSize((scoreDetailInnerFrame->width()-2)*0.5,scoreDetailInnerFrame->height()-18*2);
    scoreDetailInnerLayout->addWidget(leftScore);

    QVBoxLayout* scoreDetailLeftLayout = new QVBoxLayout(leftScore);
    scoreDetailLeftLayout->setAlignment(Qt::AlignCenter);
    scoreDetailLeftLayout->setSpacing(5);
    scoreDetailLeftLayout->setMargin(0);
    leftScore->setLayout(scoreDetailLeftLayout);

    scoreDetailLeftLayout->addWidget(BuildScoreDetailItem(leftScore,160,20,QString::fromUtf8("基础得分"),2));
    scoreDetailLeftLayout->addWidget(BuildScoreDetailItem(leftScore,160,20,QString::fromUtf8("段位差距得分"),28));
    if(bWin)
    {
        scoreDetailLeftLayout->addWidget(BuildScoreDetailItem(leftScore,160,20,QString::fromUtf8("当日首胜得分"),7));
    }

    //分割条
    QFrame* seperator = new QFrame(scoreDetailInnerFrame);
    seperator->setFixedSize(1,scoreDetailInnerFrame->height()-18*2);
    seperator->setObjectName("seperator");
    scoreDetailInnerLayout->addWidget(seperator);

    //右侧分数详情面板
    QFrame* rightScore = new QFrame(scoreDetailInnerFrame);
    rightScore->setFixedSize((scoreDetailInnerFrame->width()-2)*0.5,scoreDetailInnerFrame->height()-18*2);
    rightScore->setObjectName("rightScore");
    scoreDetailInnerLayout->addWidget(rightScore);
    scoreDetailLayout->addWidget(scoreDetailInnerFrame);

    QVBoxLayout* scoreDetailRightLayout = new QVBoxLayout(rightScore);
    scoreDetailRightLayout->setAlignment(Qt::AlignCenter);
    scoreDetailRightLayout->setSpacing(5);
    scoreDetailRightLayout->setMargin(0);
    rightScore->setLayout(scoreDetailRightLayout);

    scoreDetailRightLayout->addWidget(BuildScoreDetailItem(rightScore,160,20,QString::fromUtf8("连胜得分"),2));
    scoreDetailRightLayout->addWidget(BuildScoreDetailItem(rightScore,160,20,QString::fromUtf8("终结对方得分"),28));
    if(bWin)
        scoreDetailRightLayout->addWidget(BuildScoreDetailItem(rightScore,160,20,QString::fromUtf8("斩杀得分"),7));
}

QFrame* CTestForm::BuildScoreDetailItem(QWidget* parent,int width,int height,QString tip,int score,int leftMargin,int rightMargin)
{
    QFrame* ScoreDetailItemFrame = new QFrame(parent);
    ScoreDetailItemFrame->setFixedSize(width,height);

    QHBoxLayout* scoreDetailItemLayout = new QHBoxLayout(ScoreDetailItemFrame);
    scoreDetailItemLayout->setAlignment(Qt::AlignCenter);
    scoreDetailItemLayout->setSpacing(0);
    scoreDetailItemLayout->setMargin(0);
    scoreDetailItemLayout->setContentsMargins(leftMargin,0,rightMargin,0);
    ScoreDetailItemFrame->setLayout(scoreDetailItemLayout);

    QLabel* lbTip = new QLabel(ScoreDetailItemFrame);
    lbTip->setAlignment(Qt::AlignLeft|Qt::AlignVCenter);
    lbTip->setText(tip);
    scoreDetailItemLayout->addWidget(lbTip);

    QSpacerItem *verticalSpacer;
    verticalSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Preferred);
    scoreDetailItemLayout->addItem(verticalSpacer);

    QLabel* lbScore = new QLabel(ScoreDetailItemFrame);
    lbScore->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
    lbScore->setText(QString::number(score));
    scoreDetailItemLayout->addWidget(lbScore);
    return ScoreDetailItemFrame;
}

QFrame* CTestForm::BuildScoreDetailItem2(QWidget* parent,int width,int height,QString tip,QString score_detail,int leftMargin,int rightMargin)
{
    QFrame* ScoreDetailItemFrame = new QFrame(parent);
    ScoreDetailItemFrame->setFixedSize(width,height);

    QHBoxLayout* scoreDetailItemLayout = new QHBoxLayout(ScoreDetailItemFrame);
    scoreDetailItemLayout->setAlignment(Qt::AlignCenter);
    scoreDetailItemLayout->setSpacing(0);
    scoreDetailItemLayout->setMargin(0);
    scoreDetailItemLayout->setContentsMargins(leftMargin,0,rightMargin,0);
    ScoreDetailItemFrame->setLayout(scoreDetailItemLayout);

    QLabel* lbTip = new QLabel(ScoreDetailItemFrame);
    lbTip->setAlignment(Qt::AlignLeft|Qt::AlignVCenter);
    lbTip->setObjectName("tip");
    lbTip->setText(tip);
    scoreDetailItemLayout->addWidget(lbTip);

    QSpacerItem *verticalSpacer;
    verticalSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Preferred);
    scoreDetailItemLayout->addItem(verticalSpacer);

    QLabel* lbScore = new QLabel(ScoreDetailItemFrame);
    lbScore->setObjectName("itemValue");
    lbScore->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
    lbScore->setText(score_detail);
    scoreDetailItemLayout->addWidget(lbScore);
    return ScoreDetailItemFrame;
}
